import axios from "axios";
import { ElMessage } from "element-plus";
import { useUserStore } from "@/store/modules/user";
import { useRouter } from "vue-router";

const service = axios.create({
	timeout: 60000000, // 请求超时时间
});

const err = (error) => {
	if (error.response) {
		const data = error.response.data;
		if (error.response.status === 401) {
			const token = window.localStorage.getItem("token");
			if (token) {
				useUserStore()
					.logout()
					.then(() => {
						setTimeout(() => {
							ElMessage.warning("登录信息已过期，请重新登录");
							useUserStore().goOutClearStorage();
						}, 1500);
					})
					.catch((err) => {
						ElMessage.warning("登录信息已过期，请重新登录");
						useUserStore().goOutClearStorage();
					});
			}
		} else if (error.response.status === 403) {
			ElMessage.error(data.path + "," + data.msg);
		} else if (error.response.status === 502 || error.response.status === 500) {
			ElMessage.error("连接服务器失败!");
		} else if (error.response.status === 429) {
			ElMessage.error("访问太过频繁，请稍后再试!");
		} else {
			ElMessage.error(error.message);
		}
	} else {
		ElMessage.error("连接服务器失败!");
	}
	return Promise.reject(error);
};

const requestRrr = (error) => {
	if (error.response) {
		const data = error.response.data;
		if (data.code === 401 || data.msg === "登录状态异常") {
			const token = window.localStorage.getItem("token");
			if (token) {
				useUserStore()
					.logout()
					.then(() => {
						setTimeout(() => {
							ElMessage.warning("登录信息已过期，请重新登录");
							useUserStore().goOutClearStorage();
						}, 1500);
					})
					.catch((err) => {
						setTimeout(() => {
							ElMessage.warning("登录信息已过期，请重新登录");
							useUserStore().goOutClearStorage();
						});
					});
			}
		} else if (data.code === 403) {
			ElMessage.error(error.response.config.url + "," + data.msg);
		} else if (data.code === 502 || data.code === 500) {
			const msg = data?.msg || "";
			ElMessage.error("操作失败!" + msg);
		} else if (data.code === 429) {
			ElMessage.error("访问太过频繁，请稍后再试!");
		} else {
			ElMessage.error(data.msg);
		}
	} else {
		ElMessage.error("连接服务器失败!");
	}
};

// request拦截器
service.interceptors.request.use((config) => {
	const token = window.localStorage.getItem("token");
	if (token) {
		config.headers.Authorization = token;
	}
	return config;
}, err);

// 响应拦截器
service.interceptors.response.use((response) => {
	const { responseType } = response.request;
	const { code, success } = response.data;
	const whiteUrlList = ["/api/server"]; // 白名单
	const blobList = []; // 文件流
	let isBlob = blobList.includes(response.config.url);
	const hasIdUrl = response.config.url.includes("?")
		? response.config.url.split("?")[0]
		: "";
	if (hasIdUrl && blobList.includes(hasIdUrl)) isBlob = true;
	if (response.config.url.includes("/gdc/file/info/down")) isBlob = true;

	if (responseType === "blob" || responseType === "arraybuffer" || isBlob) {
		return isBlob ? response : response.data;
	}
	if (
		code === 200 ||
		success === "true" ||
		whiteUrlList.includes(response.config.url)
	) {
		return Promise.resolve(response.data);
	}
	// 使用Promise.reject 响应
	requestRrr({ response });
	return Promise.reject(response.data);
}, err);

export { service as request };
